package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.base.Setting;
import com.huanwei.cc.newbean.LoginT;
import com.huanwei.cc.newbean.Teacher;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 课程管理
 */
public class Curriculum_Activity extends AppCompatActivity {


    List<String> yuanxilist = Arrays.asList(Setting.yuanxi);
    List<String> banjilist = Arrays.asList(Setting.banji);
    List<String> time_duanlist = Arrays.asList(Setting.time_duan);
    List<String> weeklist = Arrays.asList(Setting.week);

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.edit_kechengname)
    EditText editKechengname;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.spinner_yuanxi)
    MaterialSpinner spinnerYuanxi;
    @BindView(R.id.shijian_lay)
    LinearLayout shijianLay;
    @BindView(R.id.spinner_banji)
    MaterialSpinner spinnerBanji;
    @BindView(R.id.spinner_laoshi)
    MaterialSpinner spinnerLaoshi;
    @BindView(R.id.spinner_xingqi)
    MaterialSpinner spinnerXingqi;
    @BindView(R.id.spinner_shijianduan)
    MaterialSpinner spinnerShijianduan;
    @BindView(R.id.but_regist)
    Button butRegist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_curr);
        ButterKnife.bind(this);
        initview();
        getTeachers();
    }

    String yuanxi;
    String banji;
    String shijianduan;
    String week,laoshi;

    private void initview() {

        yuanxi = yuanxilist.get(0);
        spinnerYuanxi.setItems(yuanxilist);
        spinnerYuanxi.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                yuanxi = yuanxilist.get(position);

            }
        });

        banji = banjilist.get(0);
        spinnerBanji.setItems(banjilist);
        spinnerBanji.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                banji = banjilist.get(position);

            }
        });
        shijianduan = time_duanlist.get(0);
        spinnerShijianduan.setItems(time_duanlist);
        spinnerShijianduan.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                shijianduan = time_duanlist.get(position);

            }
        });
        week = weeklist.get(0);
        spinnerXingqi.setItems(weeklist);
        spinnerXingqi.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                week = weeklist.get(position);

            }
        });

        spinnerLaoshi.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                laoshi = teachers.get(position);

            }
        });

    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:

                String shangkeName = edName.getText().toString();
                if (TextUtils.isEmpty(shangkeName)) {
                    Toast.makeText(this, "请输入上课地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                String Kechengname = editKechengname.getText().toString();
                if (TextUtils.isEmpty(Kechengname)) {
                    Toast.makeText(this, "请输入课程名称", Toast.LENGTH_SHORT).show();
                    return;
                }


//                API.addCurriculum(Kechengname, laoshi, week, shijianduan, shangkeName, yuanxi, banji, new Okhttp.Objectcallback() {
//                    @Override
//                    public void onsuccess(String st) {
//                        Log.e("111", "xx"+st);
//
//
//                        LoginT login = JSONUtils.parseJSON(st, LoginT.class);
//                        if (login.isIssuccess() == true) {
//                            Toast.makeText(Curriculum_Activity.this, "添加课程成功！", Toast.LENGTH_SHORT).show();
//
////                            finish();
//                        } else {
//                            Toast.makeText(Curriculum_Activity.this, login.getMsg(), Toast.LENGTH_SHORT).show();
//
//                        }
//                    }
//
//                    @Override
//                    public void onFalia(int code, String errst) {
//                        Toast.makeText(Curriculum_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
//                    }
//                });


                break;
        }
    }

    List<String> teachers = new ArrayList<String>();

    public void getTeachers() {
        API.getTeachers(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx"+st);


                Teacher teacher = JSONUtils.parseJSON(st, Teacher.class);
                if (teacher.isIssuccess() == true) {

                    List<Teacher.DataBean> ccList = teacher.getData();

                    for (int i = 0; i < ccList.size(); i++) {
                        teachers.add(ccList.get(i).getUser_name());
                    }
                    laoshi = teachers.get(0);
                    spinnerLaoshi.setItems(teachers);

                } else {
                    Toast.makeText(Curriculum_Activity.this, teacher.getMsg(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(Curriculum_Activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
